from enum import Enum

from .trigger import AlertTrigger


# 联动进入报警器
class PassTrigger(AlertTrigger):

    class Direction(Enum):
        UP = 0
        RIGHT = 1
        DOWN = 2
        LEFT = 3

    def __init__(self, event, object, region, condition):
        super().__init__(event, object, region)
        if condition == '向上经过':
            self._direction = self.Direction.UP
        elif condition == '向下经过':
            self._direction = self.Direction.DOWN
        elif condition == '向左经过':
            self._direction = self.Direction.LEFT
        elif condition == '向右经过':
            self._direction = self.Direction.RIGHT
        else:
            raise RuntimeError(f'不支持该经过条件：{condition}')
    
    def leave(self, obj, objs, image):
        # 当有物体离开时检查是否满足经过报警条件
        leave_pos = obj.get_pos()
        alert = False
        if self._direction == self.Direction.UP:
            alert = leave_pos[0] - obj.enter_pos[0] < 0
        elif self._direction == self.Direction.DOWN:
            alert = leave_pos[0] - obj.enter_pos[0] > 0
        elif self._direction == self.Direction.LEFT:
            alert = leave_pos[1] - obj.enter_pos[1] < 0
        elif self._direction == self.Direction.RIGHT:
            alert = leave_pos[1] - obj.enter_pos[1] > 0
        if alert:
            self._alert([obj], obj.make_shotcut())
